//
//  WBHomeController.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/6.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit
import SDWebImage

fileprivate let identifier = "home"
fileprivate let identifier1 = "home1"

class WBHomeController: WBRootController {
    /// viewModel
    lazy var viewModel = WBStatusListViewModel()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
        
        //接收图片点击的通知
        NotificationCenter.default.addObserver(self, selector: #selector(clickImage(notification:)), name: clickStatusImageNotification, object: nil)
    }
}

// MARK: - 设置UI
extension WBHomeController {
    func setupUI () {
        tableView.register(WBStatusCell.self, forCellReuseIdentifier: identifier)
        tableView.register(WBRetweetedCell.self, forCellReuseIdentifier: identifier1)
    }
}

// MARK: - 加载数据
extension WBHomeController {
    
    @objc override func loadData() {
        var isPullDown = true
        if refreshHeader.isRefreshing {
            isPullDown = true
        } else {
            isPullDown = false
        }
        
        //调用viewModel获取数据，获取完数据之后更新UI
        viewModel.loadData(isPullDown: isPullDown) { (success) in
            
            self.tableView.reloadData()

            //让菊花停止转动
            if isPullDown {
                self.refreshHeader.endRefreshing()
            } else {
                self.refreshFooter.endRefreshing()
            }
        }
    }
}

// MARK: - 重写父类的数据源和代理方法
extension WBHomeController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataSourceArr.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //判断使用哪一种cell
        let statuViewModel:WBStatusViewModel = viewModel.dataSourceArr[indexPath.row]
        
        
        if statuViewModel.statusModel.retweeted_status != nil {
            let cell = tableView.dequeueReusableCell(withIdentifier: identifier1, for: indexPath) as! WBRetweetedCell
            cell.statusViewModel = statuViewModel
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! WBStatusCell
            cell.statusViewModel = statuViewModel
            return cell
        }

    }
    
//    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        let statuViewModel = viewModel.dataSourceArr[indexPath.row]
//        return statuViewModel.rowHeight
//    }
}

// MARK: - 事件处理
extension WBHomeController {
    @objc func clickImage(notification: Notification) {        
        //推出pageViewController
        if let userInfo = notification.userInfo, let index = userInfo["index"] as? Int,  let picurls = userInfo["urls"] as? [WBStatusPicModel] {
            //创建图片浏览控制器
            let photoBroser = WBPicBroserController(currentIndex: index, pictureUrls: picurls)
            //推出
            present(photoBroser, animated: false, completion: nil)
        }
    }
}
